package com.hadoop.iputformat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * Created by yu756 on 2020/1/18 18:33
 */
public class WholeRecordReader extends RecordReader<Text, BytesWritable> {

    FileSplit split;

    Configuration configuration;
    Text k;
    BytesWritable v;
    boolean isProgress = true;

    public void initialize(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {

        // 初始化
        this.split = (FileSplit) split;
        configuration = context.getConfiguration();


    }

    public boolean nextKeyValue() throws IOException, InterruptedException {

        if (isProgress) {
            // 业务逻辑
            // 获取fs对象
            Path path = split.getPath();
            FileSystem fileSystem = path.getFileSystem(configuration);

            // 获取输入流

            FSDataInputStream fis = fileSystem.open(path);

            byte[] buf = new byte[(int) split.getLength()];
            // 拷贝
            IOUtils.readFully(fis, buf, 0, buf.length);

            // 封装V
            v.set(buf, 0, buf.length);

            // 封装 K
            k.set(path.toString());

            // 关闭资源
            IOUtils.closeStream(fis);
            isProgress = false;
            return true;
        }

        return false;
    }

    public Text getCurrentKey() throws IOException, InterruptedException {
        return k;
    }

    public BytesWritable getCurrentValue() throws IOException, InterruptedException {


        return v;
    }

    public float getProgress() throws IOException, InterruptedException {


        return 0;
    }

    public void close() throws IOException {

    }
}
